<%--
    Document   : index
    Created on : Mar 5, 2013, 8:58:23 AM
    Author     : ThanhTai
--%>

<%@page import="DTO.Customers.Customer"%>
<%@page import="BLO.ProductsBLO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title>Checkout cart</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />

    </head>
    <body onload="loadCartShipping();">
        <div id="wrapper">
            <jsp:include page="indexHeader.jsp"></jsp:include>
            <script>
                function stringToXML(text){
                    var doc;
                    try{
                        if(window.ActiveXObject){
                            doc = new ActiveXObject("Microsoft.XMLDOM");
                            doc.async = 'false';
                            doc.loadXML(text);

                        }else{
                            var parser = new DOMParser();
                            doc = parser.parseFromString(text,'text/xml')
                        }
                    }catch(e){
                        doc.load(text);
                    }
                    return doc;
                }

                function convertText(doc){
                    var xmlContent;
                    try{
                        if(window.ActiveXObject){
                            xmlContent = doc.documentElement.xml;
                        }else{
                            xmlContent = new XMLSerializer().serializeToString(doc);
                        }
                        return xmlContent;
                    }catch(e){
                        return "";
                        e.toString();
                    }
                }

                function editQuantity(proId){
                    var quantity = document.getElementById(proId).value;
                    RE_QUANTITY = /^[\d]*$/;
                    if(!RE_QUANTITY.test(quantity)){
                        alert('Please enter correct quanlity !!!');
                        return;
                    }
                    var xmlDOM = stringToXML(sessionStorage.xmldom);
                    if(xmlDOM != null){
                        var childs = xmlDOM.firstChild.childNodes;
                        for(var i=0;i<childs.length;i++){
                            var productId = childs[i].firstChild.firstChild.nodeValue;
                            if(productId == proId){
                                var price = childs[i].firstChild.nextSibling.nextSibling.firstChild.nodeValue;
                                var nodeQty = childs[i].firstChild.nextSibling.nextSibling.nextSibling;
                                nodeQty.firstChild.nodeValue = quantity;
                                var nodeSubPrice = nodeQty.nextSibling;
                                nodeSubPrice.firstChild.nodeValue = parseFloat(price) * quantity;
                                sessionStorage.xmldom = convertText(xmlDOM);
                                loadCartShipping();
                                return true;
                            }
                        }
                    }
                }

                function loadCartShipping(){
                    if(sessionStorage.xmldom){
                        xmlDOM = stringToXML(sessionStorage.xmldom);
                        if (xmlDOM != null){
                            sessionStorage.numOfProduct = parseInt(GetNumOfProduct(xmlDOM));
                            document.getElementById("cart-total").innerHTML = sessionStorage.numOfProduct + " Product(s)";
                            loadXML('xsl/cart.xsl', function complete(xsltDOM) {
                                transformXMLwithXSLT(xmlDOM,xsltDOM,'cart_content');
                            });
                            loadXML('xsl/shipping.xsl', function complete(xsltDOM) {
                                transformXMLwithXSLT(xmlDOM,xsltDOM,'checkCart');
                            });
                            var a = document.getElementById('linkPDF'); //or grab it by tagname etc
                            a.href = "ConvertToPDF?xmlContext=" + sessionStorage.xmldom;
                        } else{
                            document.getElementById('body').innerHTML = "";
                            document.getElementById('cart').innerHTML = "<div class=\"heading\" id=\"heading\" onmouseover=\"moveIn();\" onmouseout=\"moveOut();\">"+
                                "<a><span id=\"cart-total\">0 Product(s)</span></a>"+
                                "</div>"+
                                "<div class=\"content\" id=\"cart_content\" onmouseover=\"moveIn();\" >"+
                                "Your cart is empty !!!"+
                                "</div>";
                        }
                    }else{
                        document.getElementById('body').innerHTML = "";
                        document.getElementById('cart').innerHTML = "<div class=\"heading\" id=\"heading\" onmouseover=\"moveIn();\" onmouseout=\"moveOut();\">"+
                            "<a><span id=\"cart-total\">0 Product(s)</span></a>"+
                            "</div>"+
                            "<div class=\"content\" id=\"cart_content\" onmouseover=\"moveIn();\" >"+
                            "Your cart is empty !!!"+
                            "</div>";
                    }
                }

                function printPDF(){
                    var a = document.getElementById('printPDF'); //or grab it by tagname etc
                    a.href = "ConvertToPDF";
                    sessionStorage.xmldom = "";
                    document.getElementById("body").innerHTML="<font style=\"font-size: 14px; font-family: Tahoma;\">Printing PDF ...</font>";
                }

                function orderCart(){
                    document.getElementById('messageOrderCart').innerHTML = "";
                    document.getElementById('messageOrderCart').innerHTML = "<font style=\"font-size: 14px; font-family: Tahoma;\">Ordering</font>";
                    var xmlDetailHttp= null;
                    try {
                        xmlDetailHttp = XMLHttpRequest();
                    }catch(e){
                        try{
                            xmlDetailHttp = new ActiveXObject("Msxml2.XMLHTTP");
                        }
                        catch(e){
                            xmlDetailHttp = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                    }
                    if(xmlDetailHttp==null){
                        alert("Your browser does not support AJAX!")
                        return;
                    }
                    try{
                        xmlDetailHttp.open("POST","CustomerController",true);
                        xmlDetailHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                        var url= "action=orderCart&xmlContext=" + sessionStorage.xmldom;
                        xmlDetailHttp.send(url);
                        // get response o SearchHotelServlet action=ViewDetail
                        xmlDetailHttp.onreadystatechange = function()
                        {
                            if(xmlDetailHttp.readyState==4 && xmlDetailHttp.status==200){
                                var check = xmlDetailHttp.responseText;
                                if(check == 'true') {
                                    //order thành công
                                    sessionStorage.xmldom = "";
                                    document.getElementById('messageOrderCart').innerHTML = "";
                                    document.getElementById('messageOrderCart').innerHTML = "<font style=\"font-size: 14px; font-family: Tahoma;\">Order is Successful</font>";
                                    document.getElementById('orderCart-button').innerHTML = "";
                                    document.getElementById('printPDF-button').innerHTML = "<a id=\"printPDF\" onclick=\"printPDF();\"><button class=\"button-Order\">Print PDF</button></a>";
                                }else if (check == 'false') {
                                    //order không thành công
                                    document.getElementById('messageOrderCart').innerHTML = "";
                                    document.getElementById('messageOrderCart').innerHTML = "<font style=\"font-size: 14px; font-family: Tahoma;\">Order is not Successful</font>";
                                }
                            }
                        }
                    }catch(e){
                        alert("error: "+ e);
                    }
                }

                function deleteProduct(productId){
                    if(xmlDOM){
                        var childs = xmlDOM.firstChild.childNodes;
                        for(var i=0;i<childs.length;i++){
                            var proID = childs[i].firstChild.firstChild.nodeValue;
                            if(proID == productId){
                                xmlDOM.firstChild.removeChild(childs[i]);
                                sessionStorage.xmldom = convertText(xmlDOM);
                                if(xmlDOM.firstChild.childNodes.length == 0){
                                    sessionStorage.numOfProduct = 0;
                                    document.getElementById("cart-total").innerHTML = sessionStorage.numOfProduct + " Product(s)";
                                    document.getElementById('cart_content').innerHTML = "Your Cart is empty !!!";
                                    document.getElementById('checkCart').innerHTML = "";
                                }else{
                                    loadCartShipping();
                                }
                                return;
                            }
                        }
                    }
                }
            </script>
            <div id="body" style="background-color: #ffffff">
                <c:set var = "user" value = "${sessionScope.USER}"></c:set>
                <c:if test = "${not empty user}">
                    <h2>Your Shopping Cart</h2>
                    <div class="customerInfo">
                        <table id="cusinfoTable">
                            <tbody>
                                <tr>
                                    <td><b>Customer Name:</b></td>
                                    <td><input type="text" name="txtEmail" value="${sessionScope.USER.custName}" readonly/></td>
                                </tr>
                                <tr>
                                    <td><b>Email:</b></td>
                                    <td>
                                        <input type="text" name="txtEmail" value="${sessionScope.USER.custEmail}" readonly/>
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Phone:</b></td>
                                    <td>
                                        <input type="text" name="txtPhone" value="${sessionScope.USER.custPhone}" readonly/>
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Address:</b></td>
                                    <td>
                                        <input type="text" name="txtAddress" value="${sessionScope.USER.custAddress}" readonly/>
                                    </td>
                                </tr>
                            </tbody>
                        </table>

                    </div>
                    <div class="checkoutCart">
                        <div id="checkCart">
                        </div>
                    </div>
                    <div class="checkoutHeader">
                        <span id="messageOrderCart" style="float: left; width: 400px; margin: 0px 0px 0px 88px">
                        </span>
                        <span id="orderCart-button" style="float: right;margin: 0px 88px 0px 0px">
                            <a id="orderCart" onclick="orderCart();"><button class="button-Order">Order</button></a>
                        </span>
                        <span id="printPDF-button" style="float: right;margin: 0px 88px 0px 0px">
                            <!--                                <a id="printPDF" onclick="printPDF();"><button class="button">Print PDF</button></a>-->
                        </span>
                        <span id="continue-button" style="float: right;margin: 0px 0px 0px 0px">
                            <a href="index.jsp"><button class="button-Continue">Continue</button></a>
                        </span>
                    </div>
                </c:if>
            </div><!--End body-->
            <div id="fotter"><center>
                    <span> ﻿© 2023 by FPT_University. All rights reserved.</span></center>
            </div><!--End fotter-->
        </div><!--End wrapper-->
    </body>
</html>
